package com.xinchuang.mapper.targetSystem;

import com.xinchuang.entity.targetSystem.ValueData;
import com.xinchuang.entity.targetSystem.target.KnowData;
import com.xinchuang.entity.targetSystem.target.KnowHeard;
import com.xinchuang.entity.targetSystem.target.KnowRelationData;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName KnowMapper
 * @Description: TODO(知识维护)
 * @Author zhangxuan
 * @Date 2022-02-22 14:51
 * @Verion V1.0
 */
public interface KnowMapper {

    @Insert(value = {
            "INSERT INTO know_heard (id,remark,create_time) ",
            "VALUES ",
            "(#{id},#{remark},CURRENT_TIMESTAMP)",
            "ON DUPLICATE KEY UPDATE remark = #{remark}"
    })
    int addKnowHeard(KnowHeard knowHeard);

    @Select("SELECT remark FROM know_heard WHERE id = #{id}")
    String getHeard(@Param("id") String id);

    @Select("SELECT id FROM target_knowledge_table WHERE id = #{id}")
    String selectTargetKnowId(@Param("id") String id);

    @Insert(value = {
            "INSERT INTO target_knowledge_table (id,rel_id,user_id,kind,name,type,index_data,remark,weapon) ",
            "VALUES ",
            "(#{id},#{relId},#{userId},#{kind},#{name},#{type},#{indexData},#{remark},#{weapon})"
    })
    int addTargetKnow(KnowData knowData);

    @Update(value = {
            "UPDATE target_knowledge_table SET ",
            "rel_id = #{relId},user_id = #{userId},kind = #{kind},name = #{name},type = #{type},index_data = #{indexData},remark = #{remark},weapon = #{weapon} ",
            "WHERE id = #{id}"
    })
    int updateTargetKnow(KnowData knowData);

    @Insert(value = {
            "<script>",
            "INSERT target_knowledge_table (id,rel_id,user_id,kind,name,type,index_data,remark,weapon) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.relId},#{item.userId},#{item.kind},#{item.name},#{item.type},#{item.indexData},#{item.remark},#{item.weapon})",
            "</foreach>",
            "</script>"
    })
    int addTargetKnows(List<KnowData> knowData);

    @Select("SELECT id FROM system_knowledge_table WHERE id = #{id}")
    String selectSystemKnowId(@Param("id") String id);

    @Insert(value = {
            "INSERT INTO system_knowledge_table (id,rel_id,user_id,kind,name,type,index_data,remark) ",
            "VALUES ",
            "(#{id},#{relId},#{userId},#{kind},#{name},#{type},#{indexData},#{remark})"
    })
    int addSystemKnow(KnowData knowData);

    @Update(value = {
            "UPDATE system_knowledge_table SET ",
            "rel_id = #{relId},user_id = #{userId},kind = #{kind},name = #{name},type = #{type},index_data = #{indexData},remark = #{remark} ",
            "WHERE id = #{id}"
    })
    int updateSystemKnow(KnowData knowData);

    @Insert(value = {
            "<script>",
            "INSERT INTO system_knowledge_table (id,rel_id,user_id,kind,name,type,index_data,remark) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.relId},#{item.userId},#{item.kind},#{item.name},#{item.type},#{item.indexData},#{item.remark})",
            "</foreach>",
            "</script>"
    })
    int addSystemKnows(List<KnowData> knowData);

    @Select("SELECT id FROM systems_knowledge_table WHERE id = #{id}")
    String selectSystemsKnowId(@Param("id") String id);

    @Insert(value = {
            "INSERT INTO systems_knowledge_table (id,rel_id,user_id,kind,name,type,index_data,remark) ",
            "VALUES ",
            "(#{id},#{relId},#{userId},#{kind},#{name},#{type},#{indexData},#{remark})"
    })
    int addSystemsKnow(KnowData knowData);

    @Update(value = {
            "UPDATE systems_knowledge_table SET ",
            "rel_id = #{relId},user_id = #{userId},kind = #{kind},name = #{name},type = #{type},index_data = #{indexData},remark = #{remark} ",
            "WHERE id = #{id}"
    })
    int updateSystemsKnow(KnowData knowData);

    @Insert(value = {
            "<script>",
            "INSERT INTO systems_knowledge_table (id,rel_id,user_id,kind,name,type,index_data,remark) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.relId},#{item.userId},#{item.kind},#{item.name},#{item.type},#{item.indexData},#{item.remark})",
            "</foreach>",
            "</script>"
    })
    int addSystemsKnows(List<KnowData> knowData);

    @Delete("DELETE FROM target_knowledge_table WHERE id = #{id}")
    int deleteTargetKnow(@Param("id") String id);

    @Delete("DELETE FROM system_knowledge_table WHERE id = #{id}")
    int deleteSystemKnow(@Param("id") String id);

    @Delete("DELETE FROM systems_knowledge_table WHERE id = #{id}")
    int deleteSystemsKnow(@Param("id") String id);

    @Select(value = {
            "<script>",
            "SELECT * FROM target_knowledge_table WHERE rel_id = #{relId} ",
            "<if test = 'userId != null'> ",
            "AND user_id = #{userId} ",
            "</if>",
            "ORDER BY create_time DESC",
            "</script>"
    })
    @Results(value = {
            @Result(column = "rel_id", property = "relId"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "index_data", property = "indexData")
    })
    List<KnowData> selectTargetKnowList(@Param("relId") String relId, @Param("userId") String userId);

    @Select(value = {
            "<script>",
            "SELECT * FROM system_knowledge_table WHERE rel_id = #{relId} ",
            "<if test = 'userId != null'> ",
            "AND user_id = #{userId} ",
            "</if>",
            "ORDER BY create_time DESC",
            "</script>"
    })
    @Results(value = {
            @Result(column = "rel_id", property = "relId"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "index_data", property = "indexData")
    })
    List<KnowData> selectSystemKnowList(@Param("relId") String relId, @Param("userId") String userId);

    @Select("SELECT id,index_data FROM target_knowledge_table WHERE id = #{id}")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    KnowData selectKnowTargetOnly(@Param("id") String id);

    @Select("SELECT id,index_data FROM system_knowledge_table WHERE id = #{id}")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    KnowData selectKnowSystemOnly(@Param("id") String id);

    @Select("SELECT id,index_data FROM systems_knowledge_table WHERE id = #{id}")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    KnowData selectKnowSystemsOnly(@Param("id") String id);

    @Select(value = {
            "<script>",
            "SELECT * FROM systems_knowledge_table WHERE rel_id = #{relId} ",
            "<if test = 'userId != null'> ",
            "AND user_id = #{userId} ",
            "</if>",
            "ORDER BY create_time DESC",
            "</script>"
    })
    List<KnowData> selectSystemsKnowList(@Param("relId") String relId, @Param("userId") String userId);

    @Insert(value = {
            "<script>",
            "INSERT INTO know_relation_table (id,rel_id,index_one,index_two,index_three,effect) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.relId},#{item.indexOne},#{item.indexTwo},#{item.indexThree},#{item.effect})",
            "</foreach>",
            "</script>"
    })
    int addKnowRelations(List<KnowRelationData> knowData);

    @Delete("DELETE FROM know_relation_table WHERE id = #{id}")
    int deleteRelation(@Param("id") String id);

    @Delete("DELETE FROM know_relation_table WHERE rel_id = #{id}")
    int deleteRelationByRelId(@Param("id") String id);

    @Select("SELECT id,rel_id,index_one,index_two,index_three,effect FROM know_relation_table WHERE rel_id = #{relId} ORDER BY create_time DESC")
    @Results(value = {
            @Result(property = "relId", column = "rel_id"),
            @Result(property = "indexOne", column = "index_one"),
            @Result(property = "indexTwo", column = "index_two"),
            @Result(property = "indexThree", column = "index_three")
    })
    List<KnowRelationData> selectKnowRelationList(@Param("relId") String relId);

    @Select(value = {
            "<script>",
            "SELECT s_id AS value,system_name AS label FROM system_master WHERE 1=1 ",
            "<if test = 'keyword != null'> AND system_name LIKE '%${keyword}%'</if>",
            "<if test = 'countryList != null'>",
            " AND stat IN (",
            "<foreach collection='countryList' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</if>",
            "<if test = 'classifyList != null'>",
            " AND (",
            "<foreach collection='classifyList' item='item' index='index' separator='OR'>",
            " user_sign LIKE '%${item}%' ",
            "</foreach>",
            ")",
            "</if>",
            " ORDER BY sort ASC",
            "</script>"
    })
    List<Map<String, Object>> selectSystemPut(String userId,
                                              @Param("keyword") String keyword,
                                              @Param("countryList") List<String> countryList,
                                              @Param("classifyList") List<String> classifyList);

    @Select(value = {
            "<script>",
            "SELECT sys_id AS value,systems_name AS label FROM systems_master WHERE 1=1 ",
            "<if test = 'keyword != null'> AND systems_name LIKE '%${keyword}%'</if>",
            "<if test = 'countryList != null'>",
            " AND stat IN (",
            "<foreach collection='countryList' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</if>",
            "<if test = 'classifyList != null'>",
            " AND (",
            "<foreach collection='classifyList' item='item' index='index' separator='OR'>",
            " user_sign LIKE '%${item}%' ",
            "</foreach>",
            ")",
            "</if>",
            " ORDER BY sort ASC",
            "</script>"
    })
    List<Map<String, Object>> selectSystemsPut(String userId,
                                               @Param("keyword") String keyword,
                                               @Param("countryList") List<String> countryList,
                                               @Param("classifyList") List<String> classifyList);

    @Select(value = {
            "<script>",
            "SELECT id FROM system_type WHERE type_name IN (",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</script>"
    })
    List<String> selectSystemTypeId(List<String> name);

    @Select(value = {
            "<script>",
            "SELECT id FROM system_type WHERE type_id IN (",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</script>"
    })
    List<String> selectSystemsTypeId(List<String> name);

    @Select("SELECT key_value AS value,key_name AS label FROM target_common_attribute WHERE sign = #{sign}")
    List<Map<String, String>> selectTargetIndexPutc(@Param("sign") int sign);

    @Select("SELECT key_value AS value,key_name AS label FROM target_special_attribute WHERE type_id = #{relId}")
    List<Map<String, String>> selectTargetIndexPuts(@Param("relId") String relId);

    @Select(value = {
            "SELECT key_value AS value,key_name AS label FROM target_common_attribute ",
            "WHERE type_id IN (SELECT target_classify FROM target_base_data ",
            "WHERE target_id IN (SELECT target_id FROM system_relation WHERE s_id = #{s_id}) AND delete_sign = 1)"
    })
    List<Map<String, String>> selectTargetIndeSystemPut(@Param("s_id") String sId);

    @Select(value = {
            "SELECT b.key_value AS value,b.key_name AS label FROM ",
            "(SELECT target_classify FROM target_base_data WHERE target_id IN (SELECT target_id FROM system_relation WHERE s_id = #{relId})) AS a ",
            "LEFT JOIN ",
            "(SELECT id,type_id,key_name,key_value FROM target_common_attribute) AS b ",
            "ON ",
            "b.type_id = a.target_classify"
    })
    List<Map<String, String>> selectSystemIndexPut(@Param("relId") String relId);

    @Select(value = {
            "SELECT b.key_value AS value,b.key_name AS label FROM ",
            "(SELECT target_classify FROM target_base_data WHERE target_id IN (SELECT target_id FROM systems_relation WHERE sys_id = #{relId})) AS a ",
            "LEFT JOIN ",
            "(SELECT id,type_id,key_name,key_value FROM target_common_attribute) AS b ",
            "ON ",
            "b.type_id = a.target_classify"
    })
    List<Map<String, String>> selectSytemsIndexPut(@Param("relId") String relId);

    @Select("SELECT id,name,type,index_data,remark,weapon FROM target_knowledge_table WHERE rel_id = #{relId} AND kind = #{kind} LIMIT 1")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    KnowData selectKnowTargetOne(@Param("relId") String targetType, @Param("kind") String kind);

    @Select("SELECT id,name,type,index_data,remark,weapon FROM target_knowledge_table WHERE rel_id = #{relId} AND kind = #{kind}")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    List<KnowData> selectKnowTargetList(@Param("relId") String targetType, @Param("kind") String kind);

    @Select(value = {
            "<script>",
            "SELECT id,name,type,index_data,remark,weapon FROM target_knowledge_table WHERE rel_id = #{relId} AND kind IN (",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>",
            ")",
            "</script>"
    })
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    List<KnowData> selectKnowTargetListByKindList(@Param("relId") String targetType, @Param("list") List<String> list);

    @Select("SELECT id,name,type,index_data,remark FROM system_knowledge_table WHERE rel_id = #{relId} AND kind = #{kind}")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    List<KnowData> selectKnowSystemOne(@Param("relId") String targetType, @Param("kind") String kind);

    @Select("SELECT id,name,type,index_data,remark FROM systems_knowledge_table WHERE rel_id = #{relId} AND kind = #{kind}")
    @Results(value = {
            @Result(column = "index_data", property = "indexData")
    })
    List<KnowData> selectKnowSystemsOne(@Param("relId") String targetType, @Param("kind") String kind);

    @Select(value = {
            "SELECT a.s_id,b.system_name,a.score,a.weight,a.goal FROM ",
            "(SELECT s_id,score,weight,goal FROM value_table WHERE rel_id = #{sysId} AND target_id IS NULL) AS a",
            "LEFT JOIN",
            "(SELECT s_id,system_name FROM system_master) AS b",
            "ON",
            "a.s_id = b.s_id"
    })
    @Results(value = {
            @Result(property = "sId", column = "s_id"),
            @Result(property = "name", column = "system_name")
    })
    List<ValueData> selectSystemsValue(@Param("sysId") String sysId);

    @Select(value = {
            "SELECT a.target_id,b.target_name,a.score,a.weight,a.goal FROM ",
            "(SELECT target_id,score,weight,goal FROM value_table WHERE rel_id = #{sysId} AND s_id = #{sId} AND sub_id IS NULL) AS a",
            "JOIN",
            "(SELECT target_id,target_name FROM target_base_data) AS b",
            "ON",
            "a.target_id = b.target_id"
    })
    @Results(value = {
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "name", column = "target_name")
    })
    List<ValueData> selectSystemValue(@Param("sysId") String sysId, @Param("sId") String sId);

    @Select(value = {
            "SELECT sub_id,know_id,score,weight,goal FROM value_table WHERE rel_id = #{sysId} AND s_id = #{sId} AND target_id = #{targetId} AND sub_id = #{sudId} AND know_id IS NOT NULL"
    })
    @Results(value = {
            @Result(property = "subId", column = "sub_id"),
            @Result(property = "knowId", column = "know_id")
    })
    List<ValueData> selectSubTargetValue(@Param("sysId") String sysId, @Param("sId") String sId, @Param("targetId") String targetId, @Param("sudId") String sudId);

    @Select(value = {
            "SELECT sub_id,know_id,score,weight,goal FROM value_table WHERE rel_id = #{sysId} AND s_id = #{sId} AND target_id = #{targetId} AND sub_id IS NOT NULL AND know_id IS NULL"
    })
    @Results(value = {
            @Result(property = "subId", column = "sub_id"),
            @Result(property = "knowId", column = "know_id")
    })
    List<ValueData> selectTargetValue(@Param("sysId") String sysId, @Param("sId") String sId, @Param("targetId") String targetId);

    @Select(value = "SELECT score FROM value_table WHERE rel_id = #{sysId} AND s_id = #{sId} AND target_id = #{targetId} AND sub_id = #{subId} AND know_id IS NULL")
    Double selectTargetSubValue(@Param("sysId") String sysId, @Param("sId") String sId, @Param("targetId") String targetId, @Param("subId")String subId);

    @Select(value = {
            "<script>",
            "SELECT id,rel_id,s_id,target_id,sub_id,know_id,score,weight,goal FROM value_table WHERE 1 = 1 AND rel_id = #{sysId} ",
            "<if test = 'sId != null'> ",
            "AND s_id = #{sId}",
            "</if>",
            "<if test = 'targetId != null'> ",
            "AND target_id = #{targetId}",
            "</if>",
            "<if test = 'targetId == null'> ",
            "AND target_id IS NULL",
            "</if>",
            "<if test = 'subId != null'> ",
            "AND sub_id = #{subId}",
            "</if>",
            "<if test = 'subId == null'> ",
            "AND sub_id IS NULL",
            "</if>",
            "<if test = 'knowId != null'> ",
            "AND know_id = #{knowId}",
            "</if>",
            "<if test = 'knowId == null'> ",
            "AND know_id IS NULL",
            "</if>",
            " LIMIT 1",
            "</script>"
    })
    @Results(value = {
            @Result(property = "relId", column = "rel_id"),
            @Result(property = "sId", column = "s_id"),
            @Result(property = "targetId", column = "target_id"),
            @Result(property = "subId", column = "sub_id"),
            @Result(property = "knowId", column = "know_id"),
    })
    ValueData selectValueOnly(@Param("sysId") String sysId,
                              @Param("sId") String sId,
                              @Param("targetId") String targetId,
                              @Param("subId") String subId,
                              @Param("knowId") String knowId);

    @Insert(value = {
            "INSERT INTO value_table (id,rel_id,s_id,target_id,sub_id,know_id,score,weight,goal) ",
            "VALUES ",
            "(#{id},#{relId},#{sId},#{targetId},#{subId},#{knowId},#{score},#{weight},#{goal})",
            "ON DUPLICATE KEY UPDATE rel_id = #{relId}, s_id = #{sId}, target_id = #{targetId}, sub_id = #{subId}, know_id = #{knowId}, score = #{score}, weight = #{weight}, goal = #{goal}"
    })
    int addValue(ValueData valueData);

    /**
     * 查询价值分析最高的目标
     *
     * @return
     */
    @Select(value = {"SELECT s_id,MAX(goal) AS goal FROM value_table WHERE rel_id = #{id} AND target_id IS NULL GROUP BY s_id"})
    Map<String, Object> selectValueMaxSystemId(@Param("id") String id);

    /**
     * 查询价值分析最高的目标
     *
     * @return
     */
    @Select("SELECT target_id,MAX(goal) AS goal FROM value_table WHERE rel_id = #{id} AND s_id = #{sId} AND target_id IS NOT NULL AND sub_id IS NULL")
    Map<String, Object> selectValueMaxsId(@Param("id") String id, @Param("sId") String sId);

    /**
     * 查询价值分析最高的目标
     *
     * @return
     */
    @Select(value = {
            "SELECT target_id,sub_id,MAX(goal) FROM value_table",
            "WHERE sub_id IS NOT NULL AND know_id IS NULL ",
            "AND rel_id = #{id} AND s_id = #{sId} AND target_id = #{targetId}",
            "GROUP BY target_id, sub_id"
    })
    Map<String, Object> selectValueMaxsIdSubId(@Param("id") String id, @Param("sId") String sId, @Param("targetId") String targetId);

    @Select(value = {
            "SELECT sub_id AS subId,MAX(score) AS score FROM value_table ",
            "WHERE sub_id IS NOT NULL AND know_id IS NULL ",
            "AND rel_id = #{id} AND s_id = #{sId} AND target_id = #{targetId}",
            "GROUP BY sub_id"
    })
    Map<String, Object> selectValueMaxId(@Param("id") String id, @Param("sId") String sId, @Param("targetId") String targetId);

    /**
     * 查询价值分析最高的目标
     *
     * @return
     */
    @Select(value = {
            "SELECT rel_id,s_id,target_id,MAX(goal) AS goal FROM value_table",
            "WHERE target_id IS NOT NULL AND sub_id IS NULL AND rel_id = #{id} ",
            "GROUP BY rel_id, s_id, target_id"
    })
    Map<String, Object> selectValueMaxTargetId(@Param("id") String id);

    @Select(value = {
            "SELECT s_id,MAX(goal) AS goal FROM value_table ",
            "WHERE target_id IS NULL AND sub_id IS NULL AND rel_id = #{id}",
            "GROUP BY s_id"
    })
    Map<String, Object> selectValueMaxSId(@Param("id") String id);

    /**
     * 查询价值分析最高的目标
     *
     * @return
     */
    @Select(value = {
            "SELECT target_id,sub_id,MAX(goal) FROM value_table ",
            "WHERE sub_id IS NOT NULL AND know_id IS NULL ",
            "AND rel_id = #{id} AND target_id = #{targetId} ",
            "GROUP BY target_id,sub_id "
    })
    Map<String, Object> selectValueMaxTargetSubId(@Param("id") String id, @Param("targetId") String targetId);

    @Select("SELECT rel_id,s_id FROM value_table WHERE s_id = #{sId} AND target_id IS NULL")
    List<Map<String, Object>> selectSystemValueData(@Param("sId") String sId);

    @Select("SELECT rel_id,s_id FROM value_table WHERE rel_id = #{relId} AND s_id = #{sId} AND target_id IS NULL")
    List<Map<String, Object>> selectSystemsValueData(@Param("relId") String relId, @Param("sId") String sId);

    @Select("SELECT rel_id,s_id,target_id FROM value_table WHERE rel_id = #{relId} AND s_id = #{sId} AND target_id = #{targetId} AND sub_id IS NULL")
    List<Map<String, Object>> selectSystemTargetValueData(@Param("relId") String relId, @Param("sId") String sId, @Param("targetId") String targetId);

    @Select("SELECT rel_id,s_id,target_id FROM value_table WHERE s_id = #{sId} AND target_id = #{targetId} AND sub_id IS NULL")
    List<Map<String, Object>> selectSystemsTargetValueData(@Param("sId") String sId, @Param("targetId") String targetId);

    @Select("SELECT rel_id,s_id,target_id FROM value_table WHERE target_id = #{targetId} AND sub_id IS NULL")
    List<Map<String, Object>> selectTargetValueData(@Param("targetId") String targetId);

    @Select("SELECT rel_id,s_id,target_id,sub_id FROM value_table WHERE target_id IS NOT NULL AND sub_id = #{sudId} AND know_id IS NULL")
    List<Map<String, Object>> selectSubValueData(@Param("sudId") String sudId);

    @Select("SELECT rel_id,s_id,target_id,sub_id FROM value_table WHERE target_id IS NOT NULL AND sub_id IS NOT NULL AND know_id = #{knowId}")
    List<Map<String, Object>> selectKnowValueData(@Param("knowId") String knowId);

    @Delete(value = "DELETE FROM value_table WHERE rel_id = #{relId} AND s_id = #{sId} AND target_id = #{targetId} AND sub_id = #{sudId}")
    int deleteSubIdRelation(@Param("relId") String relId, @Param("sId") String sId, @Param("targetId") String targetId, @Param("sudId") String sudId);

    @Delete(value = "DELETE FROM value_table WHERE rel_id = #{relId} AND s_id = #{sId} AND target_id = #{targetId}")
    int deleteTargetIdRelation(@Param("relId") String relId, @Param("sId") String sId, @Param("targetId") String targetId);

    @Delete(value = "DELETE FROM value_table WHERE rel_id = #{relId} AND s_id = #{sId}")
    int deleteSystemIdRelation(@Param("relId") String relId, @Param("sId") String sId);

    /**
     * 查询武器名称通过ID
     *
     * @param id
     * @return
     */
    @Select("SELECT weapon_name FROM weapon_table WHERE id = #{id}")
    String selectWeaponName(@Param("id") String id);

    /**
     * 查询能力
     *
     * @param id
     * @param targetId
     * @return
     */
    @Select(value = {
            "<script>",
            "SELECT b.kind AS kind,SUM(a.goal) AS value FROM ",
            "(SELECT know_id,goal FROM value_table WHERE rel_id = #{id} ",
            "<if test = 'targetId != null'>AND target_id = #{targetId} </if>",
            "AND sub_id IS NOT NULL AND know_id IS NOT NULL) AS a ",
            "JOIN ",
            "(SELECT id,kind FROM target_knowledge_table WHERE kind IN ('打击能力','通讯能力','防御能力','抗灾能力','起降能力')) AS b ",
            "ON ",
            "a.know_id = b.id WHERE b.kind IS NOT NULL GROUP BY b.kind",
            "</script>"
    })
    List<Map<String, Object>> selectYHInit(@Param("id") String id, @Param("targetId") String targetId);

    @Select(value = {
            "<script>",
            "SELECT know_id,goal FROM value_table WHERE rel_id = #{id} ",
            "<if test = 'targetId != null'> AND target_id = #{targetId} </if>",
            "AND sub_id IS NOT NULL AND know_id IS NOT NULL",
//            "AND know_id IN ('1','2','3','4','5') GROUP BY know_id",
            "</script>"
    })
    List<Map<String, String>> selectYHInitWeapon(@Param("id") String id, @Param("targetId") String targetId);

    /**
     * 查询能力
     *
     * @param id
     * @param targetId
     * @return
     */
    @Select(value = {
            "<script>",
            "SELECT b.kind AS kind,SUM(a.goal) AS value FROM ",
            "(SELECT know_id,goal FROM value_table WHERE rel_id = #{id} AND s_id = #{sId} ",
            "<if test = 'targetId != null'>AND target_id = #{targetId} </if>",
            "AND sub_id IS NOT NULL AND know_id IS NOT NULL) AS a ",
            "JOIN ",
            "(SELECT id,kind FROM target_knowledge_table WHERE kind IN ('打击能力','通讯能力','防御能力','抗灾能力','起降能力')) AS b ",
            "ON ",
            "a.know_id = b.id WHERE b.kind IS NOT NULL GROUP BY b.kind",
            "</script>"
    })
    List<Map<String, Object>> selectYHSystemInit(@Param("id") String id, @Param("sId") String sId, @Param("targetId") String targetId);

    /**
     * 查询体系分析中系统包含的目标列表
     *
     * @return
     */
    @Select("SELECT target_id FROM value_table WHERE rel_id = #{id} AND s_id = #{sId} AND target_id IS NOT NULL AND sub_id IS NULL")
    List<String> valueSystemsSystemTargetList(@Param("id") String id, @Param("sId") String sId);

    /**
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     * @author zhangxuan
     * @Description TODO 查询目标知识维护类型包含字段
     * @Date 10:58 2022/4/20
     * @Param [name]
     **/
    @Select(value = {
            "<script>",
            "SELECT id,index_data,remark FROM target_knowledge_table ",
            "WHERE index_data LIKE '%${name}%' AND type = #{type} ",
            "<if test = 'status == 1'>AND rel_id IN (SELECT type_id FROM target_type WHERE parent_id IN (1)) </if>",
            "<if test = 'status == 2'>AND rel_id IN (SELECT type_id FROM target_type WHERE parent_id NOT IN (0,1)) </if>",
            "</script>"
    })
    @Results(value = {
            @Result(property = "indexData", column = "index_data"),
            @Result(property = "remark", column = "remark")
    })
    List<KnowData> selectKnowIndexAndRemark(@Param("name") String name, @Param("type") int type, @Param("status") int status);

    /**
     *@author zhangxuan
     *@Description TODO 查询目标知识维护主目标特有属性
     *@Date 11:30 2022/4/21
     *@Param [typeId]
     *@return java.util.List<com.zkwc.targetmanage.entity.target.KnowData>
     **/
    @Select(value = {
            "SELECT id,index_data,remark FROM target_knowledge_table WHERE type = 2 AND rel_id = #{typeId}"
    })
    @Results(value = {
            @Result(property = "indexData", column = "index_data"),
            @Result(property = "remark", column = "remark")
    })
    List<KnowData> selectKnowIndexAndRemarkByMainSpecial(@Param("typeId") String typeId);

    /**
     * @return int
     * @author zhangxuan
     * @Description TODO 保存目标知识维护类型包含字段
     * @Date 11:12 2022/4/20
     * @Param [knowData]
     **/
    @Update(value = {
            "UPDATE target_knowledge_table SET ",
            "index_data = #{indexData},",
            "remark = #{remark} ",
            "WHERE id = #{id}"
    })
    int updateKnowIndexAndRemark(KnowData knowData);

    /**
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     * @author zhangxuan
     * @Description TODO 查询系统知识维护类型包含字段
     * @Date 10:58 2022/4/20
     * @Param [name]
     **/
    @Select(value = {
            "SELECT id,index_data,remark FROM system_knowledge_table ",
            "WHERE index_data LIKE '%${name}%' AND type = #{type}"
    })
    @Results(value = {
            @Result(property = "indexData", column = "index_data"),
            @Result(property = "remark", column = "remark")
    })
    List<KnowData> selectSystemKnowIndexAndRemark(@Param("name") String name, @Param("type") int type);

    /**
     *@author zhangxuan
     *@Description TODO 查询系统知识维护主目标特有属性
     *@Date 11:30 2022/4/21
     *@Param [typeId]
     *@return java.util.List<com.zkwc.targetmanage.entity.target.KnowData>
     **/
    @Select(value = {
            "SELECT id,index_data,remark FROM system_knowledge_table WHERE rel_id IN (SELECT id FROM system_type WHERE status = 1 AND type_id = #{typeId}) AND type = 2"
    })
    @Results(value = {
            @Result(property = "indexData", column = "index_data"),
            @Result(property = "remark", column = "remark")
    })
    List<KnowData> selectSystemKnowIndexAndRemarkByMainSpecial(@Param("typeId") String typeId);

    /**
     * @return int
     * @author zhangxuan
     * @Description TODO 保存系统知识维护类型包含字段
     * @Date 11:12 2022/4/20
     * @Param [knowData]
     **/
    @Update(value = {
            "UPDATE system_knowledge_table SET ",
            "index_data = #{indexData},",
            "remark = #{remark} ",
            "WHERE id = #{id}"
    })
    int updateSystemKnowIndexAndRemark(KnowData knowData);

    @Delete(value = {
            "DELETE FROM systems_relationship",
            "WHERE sys_id IN (SELECT sys_id FROM systems_master WHERE include_type LIKE '%${sId}%')",
            "AND (target_one = #{targetId} OR target_two = #{targetId})"
    })
    int deleteSystemsRelation(@Param("sId") String sId, @Param("targetId") String targetId);

}
